import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class NGuessNumber
{

    public static int[] generateAnswer()
    {
        int[] answer = new int[4];

        for (int i = 0; i < answer.length; i++)
        {
            answer[i] = -1;
        }

        int index = 0;

        while(index < answer.length)
        {
            Random rd = new Random();

            int num = rd.nextInt(10);

            if(judge(answer, num))
            {
                answer[index++]=num;
            }
        }

        // System.out.println(Arrays.toString(answer));

        return answer;
    }

    public static boolean judge(int[] arr, int num)
    {
        for (int i = 0; i < arr.length; i++)
        {
            if(arr[i] == num)
            {
                return false;
            }
        }

        return true;
    }

    public static int[] getPlayerInput()
    {
        int[] guess = new int[4];

        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < 4; i++)
        {
            int temp = sc.nextInt();

            if (temp >= 0 && temp <= 9)
            {
                guess[i] = temp;
            }
        }

        System.out.println(Arrays.toString(guess));


        return guess;
    }

    public static int[] compareGuessAnswer(int[] answer, int[] guess)
    {
        int[] arr_AB = new int[2];

        int A = 0;
        int B = 0;

        // A :
        for (int i = 0; i < 4; i++)
        {
            if (guess[i] == answer[i])
            {
                A++;
            }
        }

        // B :
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (guess[i] == answer[j])
                {
                    B++;
                }
            }
        }


        arr_AB[0] = A;
        arr_AB[1] = B;

        System.out.println("A = " + arr_AB[0] + ", B = " + arr_AB[1]);

        return arr_AB;
    }

    public static void main(String[] args)
    {

        String against = "";
        Scanner sc = new Scanner(System.in);

        do
        {
            System.out.println("欢迎玩新猜数字游戏, 请猜四个不重复的[0, 9]之间的整数, 非法的猜测数字默认用0代替");
            int[] answer = generateAnswer();

            while (true)
            {
                int[] guess = getPlayerInput();

                if (isWin(answer, guess))
                {
                    System.out.println("你成功了");
                    System.out.println("是否重新开始? 输入y重新玩, 任意键退出");
                    against = sc.next();
                    break;
                }
                else
                {
                    System.out.println("请继续猜测");
                }
            }

        }
        while (against.equalsIgnoreCase("y"));


        sc.close();

    }

    public static boolean isWin(int[] answer, int[] guess)
    {
        int A = compareGuessAnswer(answer, guess)[0];

        if (A == 4)
        {
            return true;
        }

        return false;
    }


}
